package com.example.runner;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.domain.entity.Article;
import com.example.service.ArticleService;
import com.example.utils.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/** 启动时预处理，将所有文章的浏览量存入redis */

/** 程序启动后，所有bean初始化完成后，会调用该方法 */
@Component
public class ViewCountRunner implements CommandLineRunner {
    @Autowired
    private ArticleService articleService;

    @Autowired
    private RedisCache redisCache;

    @Override
    public void run(String... args) throws Exception {
        Map<String,Integer> viewCountMap=new HashMap<>();

        List<Article> articleList = articleService.list(null);

        for(Article article:articleList) {
            viewCountMap.put(article.getId().toString(),article.getViewCount().intValue());
            System.out.println(article);
        }
        redisCache.setCacheMap("ArticleViewCount",viewCountMap);
       // System.out.println("项目启动");
    }
}
